package com.feng.controller;

import com.feng.pojo.Course;
import com.feng.pojo.User;
import com.feng.service.course.CourseService;
import com.feng.service.course.CourseServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/frame")
public class FrameServet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        User user = (User) req.getSession().getAttribute("USER");

        CourseService courseService = new CourseServiceImpl();
        List<Course> courses = new ArrayList<Course>();
        req.setAttribute("realname",user.getRealname());
        if (user.getRole().equals("1")) {
            courses = courseService.queryNotElective(user.getUsername());
            req.setAttribute("username", user.getUsername());
            req.setAttribute("list", courses);
            req.getRequestDispatcher("jsp/studentFrame.jsp").forward(req, resp);
        } else {
            courses = courseService.queryAllCourse();
            req.setAttribute("list", courses);
            req.getRequestDispatcher("jsp/teacherFrame.jsp").forward(req, resp);

        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

